/**
 * 
 */
package com.uit.project.bustrip.bo;

import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import android.content.Context;
import com.uit.project.bustrip.appconfig.AppsConfig;
import com.uit.project.bustrip.bointerface.SeatBO;
import com.uit.project.bustrip.dao.SeatDaoImpl;
import com.uit.project.bustrip.daointerface.SeatDao;
import com.uit.project.bustrip.entity.Seat;
import com.uit.project.bustrip.ultil.JSONUltil;

/**
 * @author 
 *
 */
public class SeatBOImpl extends BaseBOImpl<Seat, Integer> implements SeatBO{
	public SeatBOImpl(){
		super();
	}
	public SeatBOImpl(Context context){
		super(context);
	}
	@Override
	public List<Seat> getAllSeatByTripAsync(Integer tripId) {
		String url = getByTripURL(tripId);
		JSONObject arrays = JSONUltil.getJSONObjectFromURL(url);
		if (arrays != null) {
			try {
				JSONArray jsonArray = arrays
						.getJSONArray(AppsConfig.JSON_OBJECT);
				SeatDao seatDao = new SeatDaoImpl();
				List<Seat> seats = new ArrayList<Seat>();
				for (int i = 0; i < jsonArray.length(); i++) {
					JSONObject jsonObject = (JSONObject) jsonArray.get(i);
					Seat seat = seatDao.getFromJSON(jsonObject);
					seats.add(seat);
				}
				return seats;
			} catch (Exception e) {
				return null;
			}
		}
		return null;
	}
	private String getByTripURL(Integer trip){
		return AppsConfig.getServiceURL()
				+ "?_module=Seat&_view=json&_action=listnocheck&_Tripid=" + trip;
	}
}

